package com.mercury.core.result.code;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.context.annotation.Description;

import java.util.stream.Stream;

/**
 * @author mercury
 * @date 2020/10/11 2:36 下午
 */
@Getter
@AllArgsConstructor
@Description("基础错误码")
public enum BaseCode implements IModuleRespCode {

    /**
     * 成功
     */
    SUCCESS("000", "SUCCESS", 200) {
        @Override
        public String getRespCode() {
            return "0";
        }
    },
    COMMON_ERROR("1", "COMMON ERROR", 200),
    CHECK_FAILED("2", "PARAMETER CHECK FAILED", 200),
    BAD_REQUEST("400", "REQUEST PARAMETER ERROR", 400),
    UNAUTHORIZED("401", "UNAUTHORIZED", 401),
    FORBIDDEN("403", "ACCESS DENIED", 403),
    NOT_FOUND("404", "URI Not Found", 404),
    METHOD_NOT_ALLOWED("405", "REQUEST METHOD ERROR", 405),
    NOT_ACCEPTABLE("406", "Not Acceptable", 406),
    CONFLICT("409", "RESOURCE CONFLICT", 409),
    UNSUPPORTED_MEDIA_TYPE("415", "UNSUPPORTED REQUEST HEADER TYPE", 415),
    UNAVAILABLE_FOR_LEGAL_REASONS("451", "GENERAL BUSINESS EXCEPTION", 451),
    SERVER_ERROR("500", "THE CURRENT SERVICE IS UNAVAILABLE, PLEASE TRY AGAIN LATER", 500),
    SERVICE_UNAVAILABLE("503", "SERVICE UNAVAILABLE", 503),
    /**
     * 基础功能错误
     */
    BASICS_FUNCTION_ERROR("530", "BASICS ERROR", 530),

    /**
     * 签名错误
     */
    SIGNED_VERIFY_FAILURE("2000", "SIGNED VERIFY FAILURE", 2000) {
        @Override
        public String getRespCode() {
            return "2000";
        }
    };

    private final String code;
    private final String message;
    private final int status;

    public static BaseCode parse(int status) {
        return Stream.of(values()).filter(baseCode -> baseCode.getStatus() == status).findFirst().orElse(SERVER_ERROR);
    }

    @Override
    public String getProjectCode() {
        return INTERNAL_PROJECT_CODE;
    }

    @Override
    public String getModuleCode() {
        return DEFAULT_MODULE_CODE;
    }
}
